﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Model.WaiGaoQiao;
using WaiGaoQiaoWeb.Models;
using WaiGaoQiaoWeb.JsonEntities;

namespace WaiGaoQiaoWeb.Controllers
{
    public class SupplierController : Controller
    {
        private SupplierInfo suppinfo = new SupplierInfo(true);

        public ActionResult SupplierIndex()
        {
            return View();
        }

        public ActionResult AddSupplier()
        {
            return View();
        }

        /// <summary>
        /// 点击修改按钮执行的方法
        /// </summary>
        /// <param name="supplierNo"></param>
        /// <returns></returns>
        public ActionResult EditSupplier(string supplierId)
        {
            SupplierVM vm = new SupplierVM();
            SupplierInfo info = new SupplierInfo();
            suppinfo = info.GetSupplierInfoOne(supplierId);
            vm.AddEdit = "Edit";
            vm.ID = suppinfo.ID;
            vm.Supplier_NO = suppinfo.Supplier_NO;
            vm.OldSupplier_NO = suppinfo.Supplier_NO;
            vm.Supplier_Name = suppinfo.Supplier_Name;
            vm.Contact_Person = suppinfo.Contact_Person;
            vm.Phone = suppinfo.Phone;
            vm.AddRess = suppinfo.AddRess;
            vm.Remark = suppinfo.Remark;
            return View(vm);
        }

        /// <summary>
        /// 获取供应商列表
        /// </summary>
        /// <param name="pvm"></param>
        /// <returns></returns>
        public ActionResult GetSupplierList(PageVM pvm)
        {
            string query = string.Empty;
            if (string.IsNullOrEmpty(pvm.query))
            {
                query = " and 1=1";
            }
            else
            {
                string[] param = pvm.query.Split(',');
                query = " and Supplier_Name like '%" + param[0] + "%' and Supplier_NO like '%" + param[1] + "%'";
            }
            IList<SupplierInfo> supplist = suppinfo.SelSupplierInfo(pvm.rp, pvm.page, query);
            JsonFlexiGridData jfgd = new JsonFlexiGridData();
            jfgd.page = pvm.page;
            jfgd.total = suppinfo._Suppliercount;
            List<string> v;
            foreach (SupplierInfo item in supplist)
            {
                #region
                v = new List<string>();
                v.Add(item.ID.ToString());
                v.Add(item.Supplier_NO);
                v.Add(item.Supplier_Name);
                v.Add(item.Contact_Person);
                v.Add(item.Phone);
                v.Add(item.AddRess);
                #endregion
                FlexiGridRow fgr = new FlexiGridRow()
                {
                    id = item.ID.ToString(),
                    cell = v
                };
                jfgd.rows.Add(fgr);
            }
            return Json(jfgd);
        }

        /// <summary>
        /// 保存供应商（新增界面下）
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ActionResult SaveSupplier(SupplierVM model)
        {
            int result = 0;
            if (ModelState.IsValid)
            {
                // Attempt to register the user
                SupplierInfo supp = new SupplierInfo()
                {
                    Supplier_NO = model.Supplier_NO,
                    Supplier_Name = model.Supplier_Name,
                    Contact_Person = model.Contact_Person,
                    Phone = model.Phone,
                    AddRess = model.AddRess,
                    Remark = model.Remark
                };
                result = suppinfo.SaveSupplier(supp);
            }
            return Json(result);
        }

        /// <summary>
        /// 保存供应商（修改界面下）
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ActionResult SaveSupplierEdit(SupplierVM model)
        {
            int result = 0;
            if (ModelState.IsValid)
            {
                // Attempt to register the user
                SupplierInfo supp = new SupplierInfo()
                {
                    ID = model.ID,
                    Supplier_NO = model.Supplier_NO,
                    Supplier_Name = model.Supplier_Name,
                    Contact_Person = model.Contact_Person,
                    Phone = model.Phone,
                    AddRess = model.AddRess,
                    Remark = model.Remark
                };
                result = suppinfo.SaveSupplierEdit(supp);
            }
            return Json(result);
        }

        /// <summary>
        /// 删除供应商
        /// </summary>
        /// <param name="roleid"></param>
        /// <returns></returns>
        public ActionResult DeleteSupplier(string id)
        {
            string strId = id.Remove(0, 1);
            int result = 0;
            result = suppinfo.DeleteSupplier(strId);
            return Json(result);
        }

        public ActionResult CheckSupplierNo(string Supplier_NO,string OldSupplier_NO,string AddEdit)
        {
            bool istongguo = suppinfo.CheckSupplierNo(Supplier_NO, OldSupplier_NO,AddEdit);
            return Json(istongguo);
        }
    }
}
